<?php

namespace Worker\Logic;

use Common\Logic\BaseLogic;

class RecommLogic extends BaseLogic
{
    private $userModel;
    private $joinModel;

    public static $typeDic = array(
        '2' => '任务赚',
        '10' => '问卷赚',
        // "3" => "分享赚",
        // "4" => "购物赚",
        // "5" => "信用赚"
    );

    public function __construct()
    {
        parent::__construct();
        $this->userModel = D('User');
        $this->joinModel = D('Join');
    }

    /**
     * 返回类型字典.
     *
     * @return array
     */
    public function getTypeDic()
    {
        return static::$typeDic;
    }

    /**
     * 6度循环获取徒弟裂变裂变.
     *
     * @param [type] $parentId
     * @param int    $maxLevel
     *
     * @return void
     */
    public function getRecommUserList($parentId, $maxLevel = 6)
    {
        if (empty($parentId)) {
            return static::$res->err('推手ID不能为空');
        }

        $userList = array();
        $ids = $parentId;

        //最多递归层级
        for ($i = 0; $i < $maxLevel; ++$i) {
            $where = array(
                'parent_id' => array('in', $ids),
                'user_state' => 1,
            );
            $itemUserList = $this->userModel->where($where)->select();

            if (empty($itemUserList)) {
                $i = $maxLevel;
                continue;
            }

            $ids = array();
            foreach ($itemUserList as $vo) {
                $ids[] = $vo['id'];
                $userList[] = $vo;
            }
        }

        return static::$res->suc('成功', $userList);
    }

    /**
     * 获取裂变徒弟全部审核通过的任务
     *
     * @param [type] $parentId
     * @param [type] $finishTime
     * @param int    $page
     * @param int    $size
     *
     * @return void
     */
    public function getRecommTaskList($parentId, $finishTime, $page = 1, $size = 50)
    {
        if (empty($parentId) || empty($finishTime)) {
            return static::$res->err('参数异常');
        }

        $userListRes = $this->getRecommUserList($parentId);
        $userList = $userListRes['Result'];
        if (empty($userList)) {
            return static::$res->err('推手没有任何徒弟');
        }

        $ids = array();
        $userDic = array();
        foreach ($userList as $vo) {
            $voUserId = $vo['id'];
            $ids[] = $voUserId;
            $userDic[$voUserId] = $vo;
        }

        $where = array(
            'user_id' => array('in', $ids),
            'state' => 3,
            'finish_time' => array(
                array('EGT', $finishTime.'-01 00:00:00'),
                array('ELT', $finishTime.'-31 23:59:59'),
                'and',
            ),
        );

        $statistics = $this->joinModel->field('count(*) as count,sum(income) as incomes')->where($where)->select();
        $list = $this->joinModel->where($where)->order('id desc')->page($page, $size)->select();

        foreach ($list as &$vo) {
            $vo['user_name'] = $userDic[$vo['user_id']]['name'];
            $vo['user_photo'] = $userDic[$vo['user_id']]['photo'];
        }

        $data = array(
            'list' => $list,
            'incomes' => $statistics[0]['incomes'],
            'count' => $statistics[0]['count'],
        );

        return static::$res->suc('成功', $data);
    }
}
